import React from 'react';
import Square from './square';
import { Grid, Tag, Space, Button } from '@arco-design/web-react';
const Row = Grid.Row;
const Col = Grid.Col;

class Board extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            squares: Array(9).fill(null),
            xIsNext: true,
        };
    }

    handleClick(i) {
        const squares = this.state.squares.slice();
        if (this.calculateWinner(squares) || squares[i]) {
            alert("游戏结束了！");
        }
        squares[i] = this.state.xIsNext ? 'X' : 'O';
        this.setState({
            squares: squares,
            xIsNext: !this.state.xIsNext
        });

    }

    clearBoard() {
        const squares = Array(9).fill(null);
        this.setState({ squares: squares });
    }

    calculateWinner(squares) {
        const lines = [
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8],
            [0, 3, 6],
            [1, 4, 7],
            [2, 5, 8],
            [0, 4, 8],
            [2, 4, 6],
        ];
        for (let i = 0; i < lines.length; i++) {
            const [a, b, c] = lines[i];
            if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
                return squares[a];
            }
        }
        return null;
    }

    
    componentDidMount(){
        console.log("获取远程数据在这里")
    }


    render() {
        console.log("渲染一次！");
        const winner = this.calculateWinner(this.state.squares);
        let status;
        if (winner) {
            status = 'Winner: ' + winner;
        } else {
            status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
        }

        return (
            <div>
                <Row>
                    <Col span={1}>    <Tag checkable color='arcoblue' defaultChecked>
                        下一个:{this.state.xIsNext ? 'X' : 'O'}
                    </Tag></Col>
                    <Col span={1}>
                        <Button type='primary' onClick={() => this.clearBoard()}>恢复</Button>
                    </Col>
                </Row>
                <Space></Space>
                <Row>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(0)} value={this.state.squares[0]}  ></Square>
                    </Col>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(1)} value={this.state.squares[1]}></Square>
                    </Col>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(2)} value={this.state.squares[2]}></Square>
                    </Col>
                </Row>
                <Space></Space>
                <Row>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(3)} value={this.state.squares[3]}></Square>
                    </Col>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(4)} value={this.state.squares[4]}></Square>
                    </Col>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(5)} value={this.state.squares[5]}></Square>
                    </Col>
                </Row>
                <Space></Space>
                <Row>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(6)} value={this.state.squares[6]}></Square>
                    </Col>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(7)} value={this.state.squares[7]}></Square>
                    </Col>
                    <Col span={1}>
                        <Square onClick={() => this.handleClick(8)} value={this.state.squares[8]}></Square>
                    </Col>
                </Row>
            </div>

        );
    }
}


export default Board;